<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <!-- <el-form-item label="仓库编号" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <!-- <el-form-item label="产品编号" prop="productId">
        <el-input
          v-model="queryParams.productId"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="产品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      
      <el-form-item label="产品规格" prop="productStandards">
        <el-input
          v-model="queryParams.productStandards"
          placeholder="请输入产品规格"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="产品单位" prop="productUnit">
        <el-input
          v-model="queryParams.productUnitName"
          placeholder="请输入产品单位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <!-- <el-form-item label="单位id" prop="deptId">
        <el-input
          v-model="queryParams.deptId"
          placeholder="请输入单位id"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item label="产品单价" prop="productPrice">
        <el-input-number
          v-model="queryParams.productPrice[0]"
          :controls="false"
          placeholder="最低价"
          class="!w-115px"
        />
        <span class="mx-5px">-</span>
        <el-input-number
          v-model="queryParams.productPrice[1]"
          :controls="false"
          placeholder="最高价"
          class="!w-115px"
        />
    </el-form-item>
      <el-form-item label="批次" prop="productBatch">
        <el-input
          v-model="queryParams.productBatch"
          placeholder="请输入批次"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="有效期" prop="productValidityDate">
        <el-date-picker
          v-model="queryParams.productValidityDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="库存数量" prop="count">
        <el-input-number
          v-model="queryParams.count[0]"
          :controls="false"
          placeholder="最小库存"
          class="!w-115px"
        />
        <span class="mx-5px">-</span>
        <el-input-number
          v-model="queryParams.count[1]"
          :controls="false"
          placeholder="最大库存"
          class="!w-115px"
        />
    </el-form-item>

      <!-- <el-form-item label="存放位置" prop="position">
        <el-input
          v-model="queryParams.position"
          placeholder="请输入存放位置"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
     <!-- <el-table-column label="编号" align="center" prop="id" /> -->
      <!-- <el-table-column label="仓库编号" align="center" prop="warehouseId" /> -->
      <!-- <el-table-column label="产品编号" align="center" prop="productId" /> -->
      <el-table-column label="产品名称" align="center" prop="productName" width="400" />
      <el-table-column label="产品规格" align="center" prop="productStandards" width="200" />
      <el-table-column label="产品单位" align="center" prop="productUnitName" width="100" />
      <!-- <el-table-column label="单位id" align="center" prop="deptId" /> -->
      <el-table-column label="产品单价" align="center" prop="productPrice" min-width="50" />
      <el-table-column label="批次" align="center" prop="productBatch" min-width="50" />
      <el-table-column
        label="有效期"
        align="center"
        prop="productValidityDate"
        :formatter="dateFormatter2"
        width="180px"
      />
      <el-table-column label="库存数量" align="center" prop="count" width="80" />
      <!-- <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      /> -->
      <!-- <el-table-column label="存放位置" align="center" prop="position" /> -->
      <el-table-column label="操作" align="center" min-width="100px" >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['spd:pri-stock:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['spd:pri-stock:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <PriStockForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { dateFormatte,dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { PriStockApi, PriStockVO } from '@/api/erp/pristock'
import PriStockForm from './PriStockForm.vue'

/** 一级库库存管理 列表 */
defineOptions({ name: 'PriStock' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<PriStockVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  warehouseId: undefined,
  productId: undefined,
  productUnit: undefined,
  productStandards: undefined,
  productName: undefined,
  deptId: undefined,
  productPrice: [],
  productBatch: undefined,
  productValidityDate: [],
  count: [],
  createTime: [],
  position: undefined,
  listingStatus: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await PriStockApi.getPriStockPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  // 清理 productPrice 空值
  if (queryParams.productPrice?.every(v => v === '')) {
    queryParams.productPrice = []
  }

  // 清理 count 空值
  if (queryParams.count?.every(v => v === '')) {
    queryParams.count = []
  }

  queryParams.pageNo = 1
  getList()
}


/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  console.log("点击",id)
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await PriStockApi.deletePriStock(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PriStockApi.exportPriStock(queryParams)
    download.excel(data, '一级库库存管理.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>